package com.tsy.server.wayline.controller;

import com.tsy.server.wayline.model.dto.WaylineJobDTO;
import com.tsy.server.wayline.model.param.CreateJobParam;
import com.tsy.server.wayline.model.param.JobQueryParam;
import com.tsy.server.wayline.model.param.UpdateJobParam;
import com.tsy.server.wayline.service.IFlightTaskService;
import com.tsy.server.wayline.service.IWaylineJobService;
import com.dji.sdk.common.HttpResultResponse;
import com.dji.sdk.common.PaginationData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.sql.SQLException;
import java.util.Set;


/**
 * @author sean
 * @version 1.1
 * @date 2022/6/1
 */
@RequestMapping("${url.wayline.prefix}/workspaces")
@RestController
public class WaylineJobController {

    @Autowired
    private IWaylineJobService waylineJobService;

    @Autowired
    private IFlightTaskService flighttaskService;

    /**
     * 为机场创建航线任务。
     * @param param 任务参数
     * @param projectId 项目ID
     * @return
     * @throws SQLException
     */
    @PostMapping("/{project_id}/flight-tasks")
    public HttpResultResponse createJob(@Valid @RequestBody CreateJobParam param,
                                        @PathVariable(name = "project_id") Long projectId) throws SQLException {
        return flighttaskService.publishFlightTask(param, projectId);
    }

    /**
     * 分页查询该工作空间下的所有任务。
     * @param projectId 项目ID
     * @return
     */
    @GetMapping("/{project_id}/jobs")
    public HttpResultResponse<PaginationData<WaylineJobDTO>> getJobs(JobQueryParam jobQueryParam,
                                                                     @PathVariable(name = "project_id") Long projectId) {
        PaginationData<WaylineJobDTO> data = waylineJobService.getJobs(projectId, jobQueryParam);
        return HttpResultResponse.success(data);
    }

    /**
     * 发送取消任务的指令。
     * @param jobIds 任务ID集合
     * @param workspaceId 工作空间ID
     * @return
     * @throws SQLException
     */
    @DeleteMapping("/{workspace_id}/jobs")
    public HttpResultResponse publishCancelJob(@RequestParam(name = "job_id") Set<String> jobIds,
                                               @PathVariable(name = "workspace_id") String workspaceId) throws SQLException {
        flighttaskService.cancelFlightTask(workspaceId, jobIds);
        return HttpResultResponse.success();
    }

    /**
     * 将该任务的媒体文件设置为立即上传。
     * @param workspaceId 工作空间ID
     * @param jobId 任务ID
     * @return
     */
    @PostMapping("/{workspace_id}/jobs/{job_id}/media-highest")
    public HttpResultResponse uploadMediaHighestPriority(@PathVariable(name = "workspace_id") String workspaceId,
                                                         @PathVariable(name = "job_id") String jobId) {
        flighttaskService.uploadMediaHighestPriority(workspaceId, jobId);
        return HttpResultResponse.success();
    }

    @PutMapping("/{workspace_id}/jobs/{job_id}")
    public HttpResultResponse updateJobStatus(@PathVariable(name = "workspace_id") String workspaceId,
                                              @PathVariable(name = "job_id") String jobId,
                                              @Valid @RequestBody UpdateJobParam param) {
        flighttaskService.updateJobStatus(workspaceId, jobId, param);
        return HttpResultResponse.success();
    }
}
